import pandas as pandas
import numpy as numpy
# plotting
import seaborn as seaborn
import matplotlib.pyplot as plot
import warnings
from sklearn import metrics
warnings.filterwarnings("ignore")

FE_train= pandas.read_csv("FE_day.csv")
# pandas display data frames as tables
X_Feature=FE_train.drop('cnt', axis = 1)
Y_Feature=FE_train['cnt'].values
columns=X_Feature.columns
#print(columns)
#split train data
from sklearn.model_selection import train_test_split

#random select 20% as test data
X_train, X_test, Y_train, Y_test = train_test_split(X_Feature, Y_Feature, random_state=33, test_size=0.2)
X_train.shape
#use StandardScaler to preprocess
from sklearn.preprocessing import StandardScaler
ss_X = StandardScaler()
ss_y = StandardScaler()

# TrainData
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)

Y_train = ss_y.fit_transform(Y_train.reshape(-1, 1))
Y_test = ss_y.transform(Y_test.reshape(-1, 1))


from sklearn.linear_model import LinearRegression

# Default
lr = LinearRegression()

# TrainData
lr.fit(X_train, Y_train)
# Predict
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)

print "The trained model is: y ={}x + {}".format(lr.coef_,
lr.intercept_)
# show
print "LinearRegression RMSE:",numpy.sqrt(metrics.mean_squared_error(Y_test, y_test_pred_lr))

#RidgeCV
import fivefold as ff
ff.BuildRidgeModel(X_Feature,Y_Feature)
ridge_alpha=ff.ridge_alpha
print(ridge_alpha)

from sklearn.linear_model import Ridge
ridge=Ridge(ridge_alpha)
ridge.fit(X_train,Y_train)
y_train_pred_ridge=ridge.predict(X_test)

print "Ridge RMSE:",numpy.sqrt(metrics.mean_squared_error(Y_test, y_train_pred_ridge))

lasso=ff.BuildLassoModel(X_Feature,Y_Feature)
lasso_alpha=ff.ridge_alpha
print(lasso_alpha)

from sklearn.linear_model import Lasso
Lasso=Lasso(lasso_alpha)
lasso.fit(X_train,Y_train)
y_train_pred_lasso=lasso.predict(X_test)

print "Lasso RMSE:",numpy.sqrt(metrics.mean_squared_error(Y_test, y_train_pred_lasso))

fs = pandas.DataFrame({"columns":list(columns), "coef_lr":list((lr.coef_.T)), "coef_ridge":list((ridge.coef_.T)), "coef_lasso":list((lasso.coef_.T))})
fs.sort_values(by=['coef_lr'],ascending=False)
print(fs)

